package org.jeecg.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.order.entity.AddtimeOrder;
import org.jeecg.modules.order.entity.Order;
import org.jeecg.modules.order.mapper.AddtimeOrderMapper;
import org.jeecg.modules.order.mapper.OrderMapper;
import org.jeecg.modules.order.service.IAddtimeOrderService;
import org.jeecg.modules.recharge.entity.Balance;
import org.jeecg.modules.recharge.entity.OrderRecharge;
import org.jeecg.modules.recharge.mapper.BalanceMapper;
import org.jeecg.modules.system.util.DateUtil;
import org.jeecg.modules.system.util.MenJinUtil;
import org.jeecg.modules.zixishi.entity.Quyu;
import org.jeecg.modules.zixishi.entity.Seat;
import org.jeecg.modules.zixishi.entity.Zixishi;
import org.jeecg.modules.zixishi.mapper.QuyuMapper;
import org.jeecg.modules.zixishi.mapper.SeatMapper;
import org.jeecg.modules.zixishi.mapper.ZixishiMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Description: 加时订单
 * @Author: jeecg-boot
 * @Date:   2024-06-20
 * @Version: V1.0
 */
@Service
public class AddtimeOrderServiceImpl extends ServiceImpl<AddtimeOrderMapper, AddtimeOrder> implements IAddtimeOrderService {

    @Autowired
    private AddtimeOrderMapper addtimeOrderMapper;
    @Autowired
    private BalanceMapper balanceMapper;
    @Autowired
    private ZixishiMapper zixishiMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SeatMapper seatMapper;
    @Autowired
    private QuyuMapper quyuMapper;

    @Override
    public IPage<AddtimeOrder> pageList(IPage<AddtimeOrder> page, QueryWrapper<AddtimeOrder> queryWrapper) {
        return this.baseMapper.pageList(page, queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> addByAdmin(AddtimeOrder addtimeOrder) throws Exception{
        addtimeOrderMapper.insert(addtimeOrder);

        if(StringUtils.equals("1",addtimeOrder.getPayStatus())){
            Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNum, addtimeOrder.getSeatorderNum()));
            order.setEndTime(DateUtil.addHours(order.getEndTime(),Integer.parseInt(addtimeOrder.getHours())));

            Seat seat = seatMapper.selectById(order.getSeatId());
            Quyu quyu = quyuMapper.selectById(order.getQuyuId());
            Zixishi zixishi = zixishiMapper.selectById(order.getZixishiId());
            String url = MenJinUtil.genQrCode(order,seat,quyu,zixishi);
            order.setQrcode(url);
            orderMapper.updateById(order);
            int minutes = DateUtil.minuteDiff(new Date(), order.getEndTime());
            minutes = minutes>0?minutes:1;
            redisUtil.del("orderseat:xudan:" + order.getOrderNum());
            redisUtil.set("orderseat:end:" + order.getOrderNum(), "", 60*(minutes));
        }

        return Result.OK("操作成功");
    }

    @Override
    public Result<String> payOrder4Balance(String userId, String orderNum) {
        AddtimeOrder addtimeOrder = addtimeOrderMapper.selectOne(new LambdaQueryWrapper<AddtimeOrder>().eq(AddtimeOrder::getSysUserId,userId).eq(AddtimeOrder::getOrderNum, orderNum));
        if(null == addtimeOrder){
            return Result.error("订单不存在，请确认");
        }
        Balance balance = balanceMapper.selectOne(new LambdaQueryWrapper<Balance>().eq(Balance::getSysUserId, addtimeOrder.getSysUserId()));
        if(null == balance){
            return Result.error("余额不足，请确认");
        }
        Float orderMoney = Float.valueOf(addtimeOrder.getMoney());
        Float money = balance.getBalance().floatValue();
        if(money < orderMoney){
            return Result.error("余额不足，请确认");
        }
        balance.setBalance(new BigDecimal(money-orderMoney));
        balanceMapper.updateById(balance);
        addtimeOrder.setPayStatus("1").setPayType("0").setPaytime(new Date());
        addtimeOrderMapper.updateById(addtimeOrder);

        Order order = orderMapper.selectOne(new LambdaQueryWrapper<Order>().eq(Order::getOrderNum, addtimeOrder.getSeatorderNum()));
        order.setEndTime(DateUtil.addHours(order.getEndTime(),Integer.parseInt(addtimeOrder.getHours())));
//		 order.setOrderMoney(new BigDecimal(order.getOrderMoney()).add(new BigDecimal(addtimeOrder.getMoney())).stripTrailingZeros().toPlainString());

        Seat seat = seatMapper.selectById(order.getSeatId());
        Quyu quyu = quyuMapper.selectById(order.getQuyuId());
        Zixishi zixishi = zixishiMapper.selectById(order.getZixishiId());
        String url = MenJinUtil.genQrCode(order,seat,quyu,zixishi);
        order.setQrcode(url);
        orderMapper.updateById(order);
        int minutes = DateUtil.minuteDiff(new Date(), order.getEndTime());
        minutes = minutes>0?minutes:1;
        redisUtil.del("orderseat:xudan:" + order.getOrderNum());
        redisUtil.set("orderseat:end:" + order.getOrderNum(), "", 60*(minutes));

        return Result.ok("支付成功");
    }
}
